﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using Vit.Common;
using Vit.Common.Providers;
using Vit.Web;
using Vit.Web.Models;
using Vit.Web.Providers.Data;

namespace Vit.AdminModule.Areas.Admin.Providers
{
    public class SqlDataSourceDataProvider : DbContextDataProvider<WebProvidersDb, DataSourceItem>
    {
        protected override IQueryable<DataSourceItem> OrderByDefault(IQueryable<DataSourceItem> query)
        {
            return query.OrderBy(o => o.CategoryName).ThenBy(o => o.ShowOrder).ThenBy(o => o.ItemName);
        }

        public override object[] GetKeys(DataSourceItem obj)
        {
            return new object[] { obj.CategoryName, obj.ItemName };
        }

        public override DataSourceItem GetByKeys(params object[] ids)
        {
            if (ids == null || ids.Length != 2)
                return null;
            string key1 = ids[0].ToString();
            string key2 = ids[1].ToString();
            return db.DataSourceItems.FirstOrDefault(o => o.CategoryName == key1 && o.ItemName == key2);
        }

        protected override bool IsCached
        {
            get
            {
                return true;
            }
        }
    }
}
